<?php

/**
 * 
 * ClientEngage: ClientEngage Visitor Voice (http://www.clientengage.com)
 * Copyright 2012, ClientEngage (http://www.clientengage.com)
 *
 * You must have purchased a valid license from CodeCanyon in order to have 
 * the permission to use this file.
 * 
 * You may only use this file according to the respective licensing terms 
 * you agreed to when purchasing this item on CodeCanyon.
 * 
 * 
 * 
 *
 * @author          ClientEngage <contact@clientengage.com>
 * @copyright       Copyright 2013, ClientEngage (http://www.clientengage.com)
 * @link            http://www.clientengage.com ClientEngage
 * @since           ClientEngage - Visitor Voice v 1.0
 * 
 */

/**
 * Layout helper
 */
class JqplotHelper extends AppHelper
{

    public $helpers = array(
        'Html' => array('className' => 'AppHtml'),
        'Time' => array('className' => 'AppTime'),
        'Number' => array('className' => 'AppNumber'),
        'Form' => array('className' => 'AppForm'),
        'Text');

    function __construct(View $view, $settings = array())
    {
        parent::__construct($view, $settings);
    }

    /**
     * 
     * @param array $data array('label' => value, [...])
     * @param array $settings
     * @return string HTML code for the pie chart
     */
    public function renderBar($data = array(), $settings = array())
    {
        $labelsData = $this->__prepareLabelsData($data);

        $html = '';
        $uId = $this->getID();
        $chartId = 'chart_' . $uId;
        $html .= '<div id="' . $chartId . '" style="height:500px; width:400px; position: relative" class="chartobject"></div>';

        $html .= $this->Html->scriptBlock("
$(document).ready(function(){
      var data_" . $uId . " = [" . $labelsData . "];
      var plot_" . $uId . " = $.jqplot('" . $chartId . "', [data_" . $uId . "], {
            series:[{renderer: $.jqplot.BarRenderer}],
            axesDefaults: {
                tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
                    tickOptions: {
                        fontSize: '10pt',
                        angle: -90
                }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer
                }
            }
            " . (isset($settings['title']) ? ", title: '" . stripslashes($settings['title']) . "'" : "") . "
        });
});           
");
        $html .= $this->Html->link(__('Convert to Image'), '#', array('class' => 'btn btn-mini btn_convert-img'));
        return $html;
    }

    /**
     * 
     * @param array $data array('label' => value, [...])
     * @param array $settings
     * @return string HTML code for the pie chart
     */
    public function renderPie($data = array(), $settings = array())
    {
        $labelsData = $this->__prepareLabelsData($data);

        $html = '';
        $uId = $this->getID();
        $chartId = 'chart_' . $uId;
        $html .= '<div id="' . $chartId . '" style="height:500px; width:400px; position: relative" class="chartobject"></div>';

        $html .= $this->Html->scriptBlock("
$(document).ready(function(){
    var data_" . $uId . " = [" . $labelsData . "];
    var plot_" . $uId . " = $.jqplot ('" . $chartId . "', [data_" . $uId . "], 
    { 
        seriesDefaults: {
            renderer: $.jqplot.PieRenderer, 
            rendererOptions: {
                showDataLabels: true
            }
        }, 
        legend: { show:true, location: 's' }
        " . (isset($settings['title']) ? ", title: '" . stripslashes($settings['title']) . "'" : "") . "
     }
    );
});            
");
        $html .= $this->Html->link(__('Convert to Image'), '#', array('class' => 'btn btn-mini btn_convert-img'));
        return $html;
    }

    private function __prepareLabelsData($data = array())
    {
        $labelsData = '';
        foreach ($data as $l => $v)
        {
            $labelsData .= '["' . $this->Text->truncate(addslashes($l), 40) . '", ' . $v . '],';
        }

        return $labelsData;
    }

}